
import com.anhdt.vo.RestObject;
import com.thoughtworks.xstream.XStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

public abstract class BaseService extends HttpServlet {

    public final String GOTO = "goto";
    public final String METHOD = "method";
    public final String TYPE_RP_JSON = "application/json;charset=UTF-8";
    public final String TYPE_RP_XML = "text/xml;charset=UTF-8";
    public final String TYPE_RP_HTML = "text/html;charset=UTF-8";

    public String ResponseJSon(Object object) {
        JSONObject result = new JSONObject(RestObject.fromData(object));
        return result.toString();
    }

    public String ResponseXML(Object object) {
        XStream xs = new XStream();
        xs.autodetectAnnotations(true);
        return xs.toXML(object);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String gotoPage = request.getParameter(GOTO);
        try {
            Page page = Page.valueOf(gotoPage.toUpperCase());
            switch (page) {
                case CATEGORIES:
                    gotoPage(gotoPage, request, response);
                    break;
                case INDEX:
                    gotoPage(gotoPage, request, response);
                    break;
                case HOME:
                    gotoPage(gotoPage, request, response);
                    break;
            }
        } catch (IllegalArgumentException e) {
            gotoPage(gotoPage = "page404", request, response);
        }

    }

    public void gotoPage(String name, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher requestDispatcher;
        requestDispatcher = request.getRequestDispatcher(name + ".jsp");
        requestDispatcher.forward(request, response);

    }

    public void runMethod(String methodName, Class[] argTypes, Object[] object) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Class<?> c = Class.forName(this.getClass().getName());
        Method method = c.getDeclaredMethod(methodName, argTypes);
        method.invoke(this, object);
    }

    public void exeMethod(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String method = request.getParameter(METHOD);
            if (null == method) {
                processRequest(request, response);
            } else {
                Class[] argTypes = {HttpServletRequest.class, HttpServletResponse.class};
                Object[] objectParam = {request, response};
                runMethod(method, argTypes, objectParam);
            }
        } catch (SQLException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            printError(response, "Exception :" + ex.getMessage());
            ex.getStackTrace();

        }
    }

    public void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
    }

    public void printError(HttpServletResponse response, String msg) throws IOException {
        try (PrintWriter out = response.getWriter()) {
            out.println(msg);
        }
    }

}

enum Page {

    CATEGORIES,
    INDEX,
    HOME,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
}
